package com.example.MatchGame;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.example.MatchGame.MatchDrawThread;
import com.example.MatchGame.MatchMenu;
import com.example.MatchGame.MatchDrawView;
import com.example.MatchGame.MatchState;

/**
 * Created by 陈勇劲 on 2017/10/3.
 */

public class MatchGameView extends SurfaceView implements SurfaceHolder.Callback{

    public static MatchState matchState;//声明静态公共借口类。为了控制各个状态的跳转
    private Resources res;
    private MatchDrawThread drawThread;//声明线程。用来进行画面刷新
    private Paint paint;
    private MatchDrawView matchActivity;
    //构造方法
    public MatchGameView(Context context) {
        super(context);
        this.matchActivity=(MatchDrawView) context;
        this.getHolder().addCallback(this);
        res=this.getResources();
        MatchGameView.matchState=new MatchMenu(res,matchActivity);//状态借口最初指向menu，也就是显示menu界面
        paint=new Paint();
        drawThread=new MatchDrawThread(this);//线程创建
    }




    @Override
    protected void onDraw(Canvas canvas) {

        MatchGameView.matchState.draw(canvas, paint);//状态的draw方法。myState指向谁就执行谁的draw方法，画所有画面
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        MatchGameView.matchState.touch(event);//状态的touch方法。myState指向谁就执行谁的touch方法，监听触摸
        return true;
    }


    //画面创建时调用
    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        drawThread.start();  //当画面创立的时候开始启动线程
    }


    //画面改变时调用
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {


    }


    //画面销毁时调用
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

}


